<?php
	class Admin_UsersController extends Admin_BaseController {

		public function getUsers() {
			$users = User::orderBy('username')->paginate(10);
			return View::make('admin.users.index', compact('users'));
		}

		public function asyncSearchUsers() {
			$rules = array(
				'searchu' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if(!$validator->fails())
			{
				$users = User::search(Input::get('searchu'))->orderBy('username')->get();
				return View::make('admin.users.partials.userlist', compact('users'));
			}
		}

		public function listCharacter() {
			$characters = Character::orderBy('char_name')->paginate(10);
			return View::make('admin.users.char', compact('characters'));
		}

		public function asyncSearchCharacter() {
			$rules = array(
				'searchc' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if(!$validator->fails())
			{
				$characters = Character::search(Input::get('searchc'))->orderBy('char_name')->get();
				return View::make('admin.users.partials.charlist', compact('characters'));
			}
		}

		public function listCharacterByLogin($login = null) {
			$characters = Character::where('account_name', '=', $login)->orderBy('char_name')->paginate(10);
			return View::make('admin.users.char', compact('characters'));	
		}

		public function asyncBan() {
			$rules = array(
				'login' => 'required|exists:l2jls.accounts,login'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			$chars = Character::where('account_name', '=', Input::get('login'))->get();

			$telnet = new Telnet();

			foreach($chars as $char) {
				$telnet->kick($char->char_name);	
			}

			Account::where('login', '=', Input::get('login'))->update(array('accessLevel' => '-100'));
			
			return '<p class="alert alert-success">'.Input::get('login').' a été ban avec succès.</p>';
		}

		public function asyncUnBan() {
			$rules = array(
				'login' => 'required|numeric|exists:l2jls.accounts,login'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			Account::where('login', '=', Input::get('login'))->update(array('accessLevel' => '0'));
			
			return '<p class="alert alert-success">'.Input::get('login').' a été ban avec succès.</p>';
		}

		public function asyncJail() {
			$rules = array(
				'id' => 'required|numeric|exists:l2jgs.characters,charId'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			$char = Character::find(Input::get('id'));

			try {				
				$telnet = new Telnet();
				$telnet->jail($char->char_name, 5000000);
				return '<p class="alert alert-success">'.$char->char_name.' a été jail avec succès.</p>';
			}
			catch (\Exception $e) {
				return '<p class="alert alert-danger">Impossible de jail '.$char->char_name.'.</p>';
			}
		}

		public function asyncUnJail() {
			$rules = array(
				'id' => 'required|numeric|exists:l2jgs.characters,charId'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			$char = Character::find(Input::get('id'));

			try {				
				$telnet = new Telnet();
				$telnet->unjail($char->char_name);
				return '<p class="alert alert-success">'.$char->char_name.' a été unjail avec succès.</p>';
			}
			catch (\Exception $e) {
				return '<p class="alert alert-danger">Impossible de unjail '.$char->char_name.'.</p>';
			}
		}

		public function asyncUnstuck() {
			$rules = array(
				'id' => 'required|numeric|exists:l2jgs.characters,charId'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			$char = Character::find(Input::get('id'));

			try {				
				$telnet = new Telnet();
				$telnet->kick($char->char_name);
			}
			catch (\Exception $e) {
			}
			$char->x = "81236";
			$char->y = "148638";
			$char->z = "-3469";
			$char->save();

			return '<p class="alert alert-success">'.$char->char_name.' a été unstuck avec succès.</p>';
		}

		public function asyncKick() {
			$rules = array(
				'id' => 'required|numeric|exists:l2jgs.characters,charId'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
			{
				return Redirect::back()->withInput()->withErrors($validator);	
			}

			try {
				$char = Character::find(Input::get('id'));

				$telnet = new Telnet();
				$telnet->kick($char->char_name);
				return '<p class="alert alert-success">'.$char->char_name.' a été kické avec succès.</p>';
			}
			catch (\Exception $e) {
				return '<p class="alert alert-danger">Impossible de kicker '.$char->char_name.'. Le joueur est bien online ?</p>';
			}
		}
	}